<?php
/**
* @package Pages-and-Items (com_pi_pages_and_items)
* @version 1.5.7
* @copyright Copyright (C) 2006-2010 Carsten Engel. All rights reserved.
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
* @author http://www.pages-and-items.com
*/

//no direct access
if(!defined('_VALID_MOS') && !defined('_JEXEC')){
	die('Restricted access');
}


?>
<link href="components/com_pi_pages_and_items/css/popup.css" rel="stylesheet" type="text/css" />
<?php


if($class_pi->get_var('itemsPageId')){//pagebrowser used in link-pop-up on item level

$pageId = $class_pi->get_var('itemsPageId');
$selectedPageId = $class_pi->get_var('selectedPageId');

//get link from menuitems
foreach($class_pi->menuitems as $row){
	if($row->id==$pageId){		
		$link = $row->link;		
	}
}

?>
<style type="text/css">

#minwidth{
	min-width: 700px;
}

</style>
<script language="JavaScript" type="text/javascript">

//var oldmenu = '';

function selectUrl(selectUrl, menuid){
	//if(oldmenu!=''){
		//alert(oldmenu);		
		//oldmenu.openTo(0, true);	
	//}
	//oldmenu = menuid;
	window.parent.document.getElementById('href').value = selectUrl;
	window.parent.document.getElementById('targetlist').options[0].selected = true;
	window.parent.document.getElementById('ispopup').checked = false;
	window.parent.document.getElementById('title').value = '';
}

</script>
<?php

}else{
//pagebrowser used in pop-up link-to-page-page
$link = urldecode($_GET['url']); 
?>
<script language="JavaScript" type="text/javascript">

var url = '';


function selectUrl(selectUrl, menutype_int){
	url = selectUrl;	
}

function parseUrl(){
	window.opener.document.getElementById('link').value = url;
	window.opener.document.getElementById('browserNav').options[0].selected = true;
	window.close();
}

</script>
<?php
}//end if = page-level-link
?>


<?php 
echo "<link href=\"components/com_pi_pages_and_items/css/pages_and_items3.css\" rel=\"stylesheet\" type=\"text/css\" />\n";
echo "<link href=\"components/com_pi_pages_and_items/css/dtree.css\" rel=\"stylesheet\" type=\"text/css\" />\n";
echo "<script src=\"components/com_pi_pages_and_items/javascript/dtree.js\" language=\"JavaScript\" type=\"text/javascript\"></script>\n";
echo "<script src=\"../includes/js/overlib_mini.js\" language=\"JavaScript\" type=\"text/javascript\"></script>\n";

//give headers in Joomla 1.5 a bit more spunk
$class_pi->spunk_up_headers_1_5();
		
if(!$class_pi->get_var('itemsPageId')){
//pagebrowser used in link-pop-up on item level
?>
		<table class="adminform">
						<tr>
							<th><?php echo _pi_lang_selectpage; ?></th>
						</tr>
						<tr>
						
    <td ><div align="right">
	 <input type="button" value="<?php echo _pi_lang_ok; ?>" onclick="parseUrl();" />&nbsp;&nbsp;<input type="button" value="<?php echo _pi_lang_cancel; ?>" onclick="window.close();" /></div>
	</td></tr>
	<tr>
	<td>	 
	
<?php
}//end if link-pop-up on item-level

//see how many loops we need
$loops = count($class_pi->menutypes);

//loop menutypes
for($m = 0; $m < $loops; $m++){	

	echo '<div class="dtree">';	
	echo '<p><a href="javascript: d'.$m.'.openAll();">'._pi_lang_open_all.'</a> | <a href="javascript: d'.$m.'.closeAll();">'._pi_lang_close_all.'</a></p>';

	//open javascript
	echo "<script type=\"text/javascript\"  type=\"text/javascript\">\n";
	echo "<!--\n";
	//echo "d".$m."_array = new array('d".$m."_array');\n";
	echo "var d".$m."_array = new Array('d".$m."_array');\n";
	echo "d$m = new dTree('d$m');\n";
	echo "d$m.add(0,-1,'";
	echo $class_pi->get_menutype_title($class_pi->menutypes[$m]);	
	echo "','','','','','',true);\n";	

	//make javascript-array from main-menu-items
	foreach($class_pi->menuitems as $row){
		if($row->menutype==$class_pi->menutypes[$m]){
			echo "d$m.add(".$row->id.",".$row->parent.",'".(addslashes($row->name))."','";
			if($row->id!=$pageId){
				if(strpos($row->link, "&Itemid=")){
					$stringItemId = "";
				}else{
					$stringItemId = "&Itemid=".$row->id;
				}
				echo "javascript: selectUrl(\'".$row->link.$stringItemId."\',\'d$m\');";
			}		
			if(((strstr($row->link, 'index.php?option=com_content&view=category&layout=blog') && $row->type=='url') || !strstr($row->link, 'index.php?option=com_content&view=category&layout=blog')) && $row->type!='content_blog_category'){
									echo "','','','components/com_pi_pages_and_items/images/link.gif','components/com_pi_pages_and_items/images/link.gif";		
								}else{
									echo "','','','components/com_pi_pages_and_items/images/page.gif','components/com_pi_pages_and_items/images/page.gif";		
								}
			echo "');\n";
			echo "d".$m."_array.push($row->id);\n";
		}	
	}	
   
	echo "document.write(d$m);\n";
	
	//if a page was already selected, make tree-menu-button selected
	if($class_pi->get_var('itemsPageId', '' )){
		//pagebrowser used in link-pop-up on item level		
		foreach($class_pi->menuitems as $row){
			if($row->id==$selectedPageId && $row->menutype==$class_pi->menutypes[$m]){			
				echo "d$m.openTo(";
				echo $row->id;
				echo ", true);\n";				
			}					
		}
	}else{
	
		echo "if(window.opener){\n";
		echo "option = window.opener.document.getElementById('option').value;\n";
		echo "if(option=='com_pi_pages_and_items'){\n";
		//if($option=='com_pi_pages_and_items'){
			echo "url = window.opener.document.getElementById('link').value;\n";
			echo "posItemId = url.indexOf('&Itemid=');\n";
			echo "if(posItemId!=-1){\n";
				//get the id of the link-page to select
				echo "startItemId = posItemId + 8;\n";
				echo "selectedPageId = url.slice(startItemId);\n";	
				//check if id is in current menutype-loop
				echo "for (i=0; i<d".$m."_array.length; i++){\n";
					echo "if(selectedPageId==d".$m."_array[i]){\n";
						echo "d".$m.".openTo(selectedPageId, true);\n";	
						//echo "alert('in=".$m." page='+selectedPageId+' array='+d".$m."_array);\n";
						echo "break;\n";	
					echo "}\n";	 
				echo "}\n";				
			echo "}\n";	
		//}	
		echo "}\n";	
		echo "}\n";
        
	}

	//close javascript
	echo "//-->\n";
	echo "</script>\n";
	
	echo '</div>';
	
}//end loop menutypes

if(!$class_pi->get_var('itemsPageId', '' )){
	//pagebrowser used in link-pop-up on item level
	?>
	</td></tr></table> 
	<?php
}
?>